<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <!-- 搜索添加区 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="queryInfo.query"
            @clear="getGoodsList"
          >
            <template #append>
              <el-button
                icon="el-icon-search"
                @click="getGoodsList"
              ></el-button>
            </template>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods"> 添加商品 </el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="goodsList" border style="width: 100%">
        <el-table-column type="index" width="50" />
        <el-table-column prop="goods_name" label="商品名称" />
        <el-table-column
          prop="goods_price"
          label="商品价格（元）"
          width="120px"
        />
        <el-table-column prop="goods_weight" label="商品重量" width="90px" />
        <el-table-column prop="add_time" label="创建时间" width="180px">
          <template #default="scope">
            {{ scope.row.add_time }}
          </template>
        </el-table-column>
        <el-table-column prop="操作" label="操作" width="130px">
          <template #default="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editusers(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import {MessageBox,Message} from "element-ui";
export default {
  name: "List",
  data() {
    return {
      // 绑定商品列表参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      // 商品列表
      goodsList: [],
      // 商品总数
      total: 0,
    };
  },
  methods: {
    // 处理时间
    // padStart(2,'0')表示没有2位用0补全
    dataFormat(time) {
      // 这里要乘以1000因为时间戳是用毫秒来计算的
      time = time * 1000;
      const dt = new Date(time);
      // console.log(dt);
      const y = dt.getFullYear();
      // console.log(y);

      const m = (dt.getMonth() + 1 + "").padStart(2, "0");
      // console.log(m);
      const d = (dt.getDate() + "").padStart(2, "0");
      const hh = (dt.getHours() + "").padStart(2, "0");
      const mm = (dt.getMinutes() + "").padStart(2, "0");
      const ss = (dt.getSeconds() + "").padStart(2, "0");
      return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
    },
    // 获取商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get("/goods", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200)
        return this.$message.error("获取商品列表失败");
      this.goodsList = res.data.goods;
      this.total = res.data.total;
      this.goodsList = this.goodsList.map((item) => {
        item.add_time = this.dataFormat(item.add_time);
        return item;
      });
    },

    // 监听页码内容大小改变
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize;
      this.getGoodsList();
    },
    // 监听页面改变
    handleCurrentChange(newnum) {
      this.queryInfo.pagenum = newnum;
      this.getGoodsList();
    },
    // 删除商品
    async deleteGoods(id) {
      const res = await MessageBox.confirm(
        "此操作将永久删除该商品,是否继续?",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch(() => {
        Message({
          type: "info",
          message: "已取消删除",
        });
      });
      // 如果用户确认了删除,返回字符串confirm
      if (res === "confirm") {
        const { data: res } = await this.$http.delete(`/goods/${id}`);
        if (res.meta.status !== 200) return this.$message.error("删除商品失败");
       Message({
          type: "success",
          message: "删除商品成功",
        });
        this.getGoodsList();
      }
    },
    // 添加商品
    addGoods() {
      this.$router.push("/goods/add");
    },
  },
  created() {
    this.getGoodsList();
  },
};
</script>

<style lang='less' scoped>
</style>